﻿using System;
using System.Windows;
using System.Windows.Controls;
using RdH.XMLToolbox.Plugin;

namespace RdH.XMLToolbox.InsertDTD
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class DTDInserterPanel : UserControl, IPlugin
    {
        public DTDInserterPanel()
        {
            InitializeComponent();
        }

        #region Events
        void itemClear_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.MessageBox.Show("Clear Test");
        }

        private void buttonSource_Click(object sender, RoutedEventArgs e)
        {
            if ((bool)chkFile.IsChecked)
            {
                Microsoft.Win32.OpenFileDialog fileBrowserDialog = new Microsoft.Win32.OpenFileDialog();

                fileBrowserDialog.CheckFileExists = true;
                fileBrowserDialog.CheckPathExists = true;
                fileBrowserDialog.Filter = "Xml Files (*.xml; *.sgml) |*.xml;*.sgml|All Files (*.*)|*.*";
                fileBrowserDialog.FilterIndex = 0;
                fileBrowserDialog.Multiselect = false;

                if ((bool)fileBrowserDialog.ShowDialog())
                {
                    textBoxSource.Text = fileBrowserDialog.FileName;
                }
            }
            else
            {
                System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();

                folderBrowserDialog.Description = "Select a folder with Xml Files to process.";

                if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    textBoxSource.Text = folderBrowserDialog.SelectedPath;
                }
            }
        }

        private void buttonDestination_Click(object sender, RoutedEventArgs e)
        {
            if ((bool)chkFile.IsChecked)
            {
                Microsoft.Win32.OpenFileDialog fileBrowserDialog = new Microsoft.Win32.OpenFileDialog();

                fileBrowserDialog.CheckFileExists = false;
                fileBrowserDialog.CheckPathExists = true;
                fileBrowserDialog.Filter = "Xml Files|*.xml;*.sgml;";
                fileBrowserDialog.Multiselect = false;

                if ((bool)fileBrowserDialog.ShowDialog())
                {
                    textBoxDestination.Text = fileBrowserDialog.FileName;
                }
            }
            else
            {
                System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();

                folderBrowserDialog.Description = "Select a folder with Xml Files to process.";

                if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    textBoxDestination.Text = folderBrowserDialog.SelectedPath;
                }
            }
        }

        private void chkFile_Checked(object sender, RoutedEventArgs e)
        {
            chkRecurse.IsChecked = false;
            chkRecurse.IsEnabled = false;
        }

        private void chkFile_Unchecked(object sender, RoutedEventArgs e)
        {
            chkRecurse.IsEnabled = true;
        }

        private void buttonProcess_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DTDInserter inserter = new DTDInserter(textBoxSource.Text, textBoxDestination.Text);
                inserter.RecurseFolder = (bool)chkRecurse.IsChecked;
                if ((bool)chkPreserve.IsChecked)
                    inserter.Process(textBoxDTD.Text, textBoxHeader.Text, textBoxHeader.Text);
                else
                    inserter.Process(textBoxDTD.Text, textBoxHeader.Text);
            }
            catch (ArgumentException error)
            {
                System.Windows.Forms.MessageBox.Show(String.Format("One of the arguments is invalid\r\nPlease check your input\r\n\r\n({0})", error.Message), "DTDinserter Exception", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
            }
        }
        #endregion

        #region IPlugin Members

        public String Title
        {
            get { return "Insert DTD"; }
        }

        public String Discription
        {
            get { return "This program will process one or more files and insert/replace the DTD reference."; }
        }

        public MenuItem MenuItem
        {
            get 
            { 
                MenuItem item = new MenuItem();
                item.Header = this.Title;
                
                MenuItem itemClear = new MenuItem();
                itemClear.Header = "Clear fields";
                itemClear.Click += new RoutedEventHandler(itemClear_Click);

                item.Items.Add(itemClear);

                return item;
            }
        }

        public UserControl Control
        {
            get { return this; }
        }

        public double MinimumHeight
        {
            get { return 300; }
        }

        public double MinimumWidth
        {
            get { return 500; }
        }

        #endregion




    }
}
